package gewu.suleju.controller;


import gewu.suleju.dao.iml.IHouseInforDao;
import gewu.suleju.dao.iml.IHousePic;
import gewu.suleju.model.pojo.BrokerInfo;
import gewu.suleju.model.pojo.HouseInfor;
import gewu.suleju.model.pojo.HousePic;
import gewu.suleju.model.vo.Result;
import gewu.suleju.sevice.IUploadHouseService;
import gewu.suleju.sevice.imp.UploadHouseServiceImp;
import gewu.suleju.util.JWTUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;


@Controller
@RequestMapping("upload")
public class UploadHouseController {
    @Autowired
    private UploadHouseServiceImp uploadHouseServiceImp;
    @Autowired
    private IHousePic iHousePic;
    @Autowired
    private IHouseInforDao iHouseInforDao;
    @PostMapping("house")
    @ResponseBody
    public Result house(@Valid gewu.suleju.model.vo.HouseInfor value, BindingResult result, HttpServletRequest request) throws ParseException {
        if(result.hasErrors()){
           System.out.println( result.getFieldError().getDefaultMessage());

        }
        //更改房东的名字、电话号码
        String token = request.getHeader("token");
        Claims claims = JWTUtil.checkJWT(token);
        Integer id = (Integer)claims.get("id");
        uploadHouseServiceImp.updateUser(value.getName(),value.getPhone(),id);
        //上传房源信息
        HouseInfor houseInfor = new HouseInfor();
        houseInfor.setRentPrice(Integer.parseInt(value.getRentPrice()));
        houseInfor.setHouseType("二室一厅");
        //日期格式化
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date= format.parse(value.getStartRentData());
        houseInfor.setStartRentDate(date);
        houseInfor.setCommunity(value.getCommunity());
        houseInfor.setHouseAddress(value.getHouseAddress());
        houseInfor.setRentType(value.getHouseType());
        houseInfor.setPayType("押一付三");
        houseInfor.setIsStatus(1);
        houseInfor.setUserId(id);
        houseInfor.setIsEntrust(Integer.parseInt(value.getEntrust()));
        houseInfor.setClickCount(0);
        //分配经纪人
        BrokerInfo brokerInfo = uploadHouseServiceImp.findByServiceArea(value.getRegion());
        houseInfor.setBrokerInfoId(brokerInfo.getId());
        houseInfor.setHouseDescribe(value.getHouseDescribe());
        houseInfor.setArea(value.getRegion());
        houseInfor.setMinRentDate("一个月起租");
        houseInfor.setFloor(value.getFloor());
        houseInfor.setElevator(value.getElevator());
        houseInfor.setDirection("朝南");
        houseInfor.setSquareMeter(Integer.parseInt(value.getArea()));
        houseInfor.setIsPic(-1);//为-1没有加载图片
        //该房源的特色
          houseInfor.setHouseFeatureId("123456");
        String featureStr = "";
        if(value.getL1()!=null){
            featureStr += 1;
        }
        if(value.getL2()!=null){
            featureStr += 2;
        }
        if(value.getL3()!=null){
            featureStr += 3;
        }
        if(value.getL4()!=null){
            featureStr += 4;
        }if(value.getL5()!=null){
            featureStr += 5;
        }
        if(value.getL6()!=null){
            featureStr += 6;
        }
        if(value.getL7()!=null){
            featureStr += 7;
        }if(value.getL8()!=null){
            featureStr += 8;
        }if(value.getL9()!=null){
            featureStr += 9;
        }if(value.getL10()!=null){
            featureStr += 10;
        }
        houseInfor.setHouseDeviceKindId(featureStr);
        houseInfor.setSubway("1号线");
        iHouseInforDao.save(houseInfor);
        return new Result();
    }
    @PostMapping("pic")
    @ResponseBody
    public Result pic(MultipartFile[] files){
        //放到rabbitmq里面执行
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }//让它晚加载2秒
        int id = uploadHouseServiceImp.isHavePic(-1);
        uploadHouseServiceImp.isPic(id);
        for(int i=0;i<files.length;i++) {
            HousePic housePic = new HousePic();
            String picName = files[i].getOriginalFilename();
            picName = UUID.randomUUID().toString() + "=" + picName;

            housePic.setIsStatus(1);
            housePic.setHouseInforId(id);
            housePic.setHousePicAddress(picName);

            //上传图片到nginx
            File f = new File("E:/JavaEE/nginx-1.22.0/html/suleju/All-Image/" + picName);
            try {
                files[i].transferTo(f);
            } catch (IOException e) {
                e.printStackTrace();
            }
            iHousePic.save(housePic);
        }
        return new Result();
    }
}